﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;

namespace fap
{
    /// <summary>
    /// Page1.xaml 的交互逻辑
    /// </summary>
    public partial class 主页辅显示 : Page
    {
        public 主页辅显示()
        {
            InitializeComponent();
        }

        Thread 读取线程;
        private void 控件初始化()
        {




        }

        private void 鼠标进入(object sender, MouseEventArgs e)
        {
            Image ig = sender as Image;
            switch (ig.Tag.ToString())
            {
                case "返回":
                    {
                        Uri ur = new Uri("/fap;component/素材/主显示页面/主页.png", UriKind.Relative);
                        BitmapImage bp = new BitmapImage(ur);
                        ig.Source = bp;


                        break;
                    }


            }
            TimeSpan timeSpan = new TimeSpan(0, 0, 0, 0, 300);
            WPF相应类.ScaleEasingAnimationShow(ig, 1, 1.2, 2, timeSpan);
        }
        private void 鼠标离开(object sender, MouseEventArgs e)
        {
            Image ig = sender as Image;
            switch (ig.Tag.ToString())
            {
                case "返回":
                    {
                        Uri ur = new Uri("/fap;component/素材/主显示页面/主页.png", UriKind.Relative);
                        BitmapImage bp = new BitmapImage(ur);
                        ig.Source = bp;


                        break;
                    }



            }
            TimeSpan timeSpan = new TimeSpan(0, 0, 0, 0, 300);
            WPF相应类.ScaleEasingAnimationShow(ig, 1.2, 1, 2, timeSpan);
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Image obj = (Image)sender;
            switch (obj.Tag)
            {
                case "返回":
                    {
                        //窗口.主页主显示.窗口初始化();
                        切换界面(0);
                        break;
                    }

            }
        }


        private List<DockPanel> grid添加控件(Grid grid, Array array, int 行数, WPFgrid类.控件类型 类型, bool 是否加点击事件)
        {
            List<DockPanel> list = new List<DockPanel>();
            int 列数 = (int)Math.Ceiling((double)array.Length / 行数);
            for (int y = 0; y < 列数; y++)
            {
                if (y == 1)
                {
                    int kjj = 1;
                }

                for (int x = 0; x < 行数; x++)
                {
                    if (array.Length > x + 行数 * y)
                    {


                        int 宽 = (int)grid.Width / 列数;
                        int 高 = (int)grid.Height / 行数;
                        switch (类型)
                        {
                            case WPFgrid类.控件类型.label:
                                {


                                    DockPanel panel = WPFgrid类.grid动态添加Label(grid, x, y, array.GetValue(x + 行数 * y).ToString(), 宽, 高, array.GetValue(x + 行数 * y).ToString());
                                    if (是否加点击事件)
                                    {
                                        panel.MouseLeftButtonDown += label_MouseLeftButtonDown;
                                        //label.MouseEnter += label_mouseenter;
                                        //label.MouseLeave += label_mouseleave;

                                    }
                                    list.Add(panel);
                                    break;
                                }


                        }

                    }
                }
            }
            return list;
        }

        private void UserControl_Initialized(object sender, EventArgs e)
        {
            Storyboard baiyun = Resources["箭头移动"] as Storyboard;
            baiyun.Begin();

            Array array = Enum.GetValues(typeof(枚举.I枚举));
            List<DockPanel> 集合 = grid添加控件(输入grid, array, 10, WPFgrid类.控件类型.label, false);
            for (int x = 0; x < 集合.Count(); x++)
            {
          
                集合[x].DataContext = 运动控制数据.I读取结果[x];
            }

            array = Enum.GetValues(typeof(枚举.Q枚举));
           集合 = grid添加控件(输出grid, array, 10, WPFgrid类.控件类型.label, true);
            for (int x = 0; x < 集合.Count(); x++)
            {
                集合[x].DataContext = 运动控制数据.Q读取结果[x];
            }

            array = Enum.GetValues(typeof(枚举.报警枚举));
           集合 = grid添加控件(报警grid, array, 10, WPFgrid类.控件类型.label, true);
            for (int x = 0; x < 集合.Count(); x++)
            {
                集合[x].DataContext = 运动控制数据.报警读取结果[x];
            }
            //读取线程 = new Thread(读取线程方法);
            //读取线程.Start();


        }

        private void label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {


            if (运动控制数据.西门子PLC.状态)
            {

                DockPanel panel = (DockPanel)sender;
                List<Label> list = WPF相应类.GetChildObjects<Label>(panel);
                Label Label1 = list[0];
                string s = Label1.Content.ToString();
                int k = 0;

                try
                {
                    运动控制数据.修改Q点位 = (枚举.Q枚举)Enum.Parse(typeof(枚举.Q枚举), s);
                    运动控制数据.Q写入 = true;
                }
                catch
                {

                }

            }
        }
        public void 切换界面(int x)
        {
            switch (x)
            {
                case 0:
                    {
                        NavigationService.Navigate(窗口.主页);

                        break;
                    }

                case 1:
                    {
                        NavigationService.Navigate(窗口.主页);

                        break;
                    }

                case 3:
                    {
                        NavigationService.Navigate(窗口.运动控制);

                        break;
                    }
                case 4:
                    {
                        NavigationService.Navigate(窗口.统计);

                        break;
                    }
                case 5:
                    {

                        break;
                    }

            }

        }

        public void 读取线程方法()
        {

            //while(true)
            //{
            //    Thread.Sleep(10);
            //    Dispatcher.Invoke((Action)(() =>
            //    {
            //        foreach (var item in 输入grid.Children)
            //        {

            //            try
            //            {
            //                Uri ur = null;

            //                DockPanel panel = (DockPanel)item;
            //                List<Label> list = WPF相应类.GetChildObjects<Label>(panel);
            //                List<Image> image = WPF相应类.GetChildObjects<Image>(panel);
            //                Label Label1 = list[0];
            //                Image image1 = image[0];
            //                string s = Label1.Content.ToString();
            //                int x = (int)Enum.Parse(typeof(枚举.M枚举), s);
            //                if (运动控制数据.I读取结果[x])
            //                {
            //                    ur = new Uri("/fap;component/素材/运动控制页面/绿灯.png", UriKind.Relative);

            //                }
            //                else
            //                {
            //                    ur = new Uri("/fap;component/素材/运动控制页面/红灯.png", UriKind.Relative);

            //                }

            //                Dispatcher.Invoke((Action)(() =>
            //                {
            //                    BitmapImage bp = new BitmapImage(ur);

            //                    image1.Source = bp;

            //                }));
            //            }
            //            catch (ArgumentException)
            //            {
            //                //unknown string or s is null
            //            }
            //            Thread.Sleep(10);

            //        }
            //    }));


            //}
        }
    }
}
